package leetcode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/**
 * ClassName: DianXinT1
 * Package: leetcode
 * Description:
 *
 * @Author wjn
 * @Create 2025/9/27 09:45
 * @Version 1.0
 */
public class DianXinT3 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int T = Integer.parseInt(br.readLine());
        for (int i = 0; i < T; i++) {
            int n = Integer.parseInt(br.readLine());
            String s = br.readLine();
            System.out.println(minSwapsToBalance(s));
        }
    }

    private static int minSwapsToBalance(String s) {
        int n = s.length();
        int balance = 0;
        int swaps = 0;

        for (int i = 0; i < n; i++) {
            if (s.charAt(i) == '(') {
                balance++;
            } else {
                balance--;
            }
            if (balance == -1) {
                int j = i + 1;
                while (j < n && s.charAt(j) != '(') {
                    j++;
                }
                swaps++;
                balance = 1;
            }

        }
        return swaps;
    }


}
